const { Article } = require('../../model/article');
const { Category } = require('../../model/Category');
const pagination = require('mongoose-sex-page');
module.exports = async (req,res)=>{
  //分类，状态都有选择
  if(req.body.classify != 'allClassify'&&req.body.state != 2) {
    let page = req.query.page;
    let category = await Category.find();
    let articles = await pagination(Article).find({classify: req.body.classify,state: req.body.state}).populate('author').populate('classify').page(page).size(5).display(3).exec();
    articles = JSON.stringify(articles);
    articles = JSON.parse(articles);
    res.render('admin/articleList',{
      articles,
      category
    });
  }
  //分类有，状态没有
  else if(req.body.classify != 'allClassify' && req.body.state == 2){
    let page = req.query.page;
    let category = await Category.find();
    let articles = await pagination(Article).find({classify: req.body.classify}).populate('author').populate('classify').page(page).size(5).display(3).exec();
    articles = JSON.stringify(articles);
    articles = JSON.parse(articles);
    res.render('admin/articleList',{
      articles,
      category
    });
  }
  //分类没有，状态有
  else if (req.body.classify == 'allClassify' && (req.body.state == 1 || req.body.state == 0)){
    let page = req.query.page;
    let category = await Category.find();
    let articles = await pagination(Article).find({state: req.body.state}).populate('author').populate('classify').page(page).size(5).display(3).exec();
    articles = JSON.stringify(articles);
    articles = JSON.parse(articles);
    res.render('admin/articleList',{
      articles,
      category
    });
  }
  //两个都没有
  else{
    let page = req.query.page;
    let category = await Category.find();
    let articles = await pagination(Article).find().populate('author').populate('classify').page(page).size(5).display(3).exec();
    articles = JSON.stringify(articles);
    articles = JSON.parse(articles);
    res.render('admin/articleList',{
      articles,
      category
    });
  }
}